package activity

import (
	"fmt"
	"gq_volunteer_platform/bean"
	"gq_volunteer_platform/model/activity"
	"gq_volunteer_platform/model/user"
	"time"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func JoinApply(c *gin.Context) {
	// 获取登录用户对象
	var u user.User
	if err := u.Get(c); err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	if u.CheckStatus != "已审核" {
		c.String(400, fmt.Sprintf("您的账号处于“%s”状态,请先通过审核", u.CheckStatus))
		return
	}

	var req struct {
		ActivityId uint `binding:"max=10000000"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		bean.Log.Warning(err.Error())
		c.String(400, err.Error())
		return
	}

	var act activity.Activity
	if err := bean.MysqlDb.Where("`id`=?", req.ActivityId).First(&act).Error; err != nil {
		bean.Log.Error(err)
		c.String(400, err.Error())
		return
	}

	if act.Status != "招募中" {
		c.String(400, fmt.Sprintf("活动状态：%s 不可加入", act.Status))
		return
	}

	// 验证人数是否已满
	if act.ParticipantCount >= act.RecruitmentNumber {
		c.String(400, "参加人数已满")
		return
	}

	var one activity.ActivityUser
	if err := bean.MysqlDb.Where("`activity_id`=?", req.ActivityId).Where("`user_id`=?", u.ID).Find(&one).Error; err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	// 已经加入队伍
	if one.ID != 0 {
		if one.CheckStatus == "已驳回" {
			c.String(400, "您加入队伍的申请已被驳回")
		} else {
			c.String(400, "您已加入队伍，请勿重复提交")
		}
		return
	}

	var two activity.ActivityUser
	two.OrganizationId = act.OrganizationId
	two.UserId = u.ID
	two.ActivityId = req.ActivityId
	two.CheckStatus = "已审核"
	two.ExitCheckStatus = "待申请"
	two.SignStatus = "未签到"

	var tx = bean.MysqlDb.Begin()

	if err := tx.Create(&two).Error; err != nil {
		tx.Rollback()
		bean.Log.Error(err)
		c.String(500, "数据库错误")
		return
	}

	if err := tx.Model(&act).Update("`participant_count`", gorm.Expr("`participant_count` + 1")).Error; err != nil {
		tx.Rollback()
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	// 提交执行
	if err := tx.Commit().Error; err != nil {
		tx.Rollback()
		bean.Log.Error(err)
		c.String(500, "数据库错误")
		return
	}

	c.PureJSON(200, one)
}

func ExitApply(c *gin.Context) {
	// 获取登录用户对象
	var u user.User
	if err := u.Get(c); err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	var req struct {
		ActivityId uint `binding:"max=10000000"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		bean.Log.Warning(err.Error())
		c.String(400, err.Error())
		return
	}

	var act activity.Activity
	if err := bean.MysqlDb.Where("`id`=?", req.ActivityId).First(&act).Error; err != nil {
		bean.Log.Error(err)
		c.String(400, err.Error())
		return
	}

	var one activity.ActivityUser
	if err := bean.MysqlDb.Where("`activity_id`=?", req.ActivityId).Where("`user_id`=?", u.ID).First(&one).Error; err != nil {
		bean.Log.Error(err)
		c.String(400, err.Error())
		return
	}

	var two activity.ActivityUser
	two.ID = one.ID
	two.ExitCheckStatus = "待审核"

	if err := bean.MysqlDb.Updates(&two).Error; err != nil {
		bean.Log.Error(err)
		c.String(500, "数据库错误")
		return
	}

	c.PureJSON(200, one)

	// // message queue
	// // 给管理员发送一条信息
	// // 查询 管理员 微信id
	// users, err := activity.ActivityFindAdminUser(act)
	// if err != nil {
	// 	bean.Log.Error(err)
	// 	return
	// }

	// var template_id = "S9WwalZ5fVvQa4t7X0th74kHOYHq-ecnTYHEvsX2X1w" //
	// var page = "index"

	// var paramData struct {
	// 	Phrase1 struct {
	// 		Value string `json:"value"`
	// 	} `json:"phrase1"`
	// 	Date2 struct {
	// 		Value string `json:"value"`
	// 	} `json:"date2"`
	// 	Name3 struct {
	// 		Value string `json:"value"`
	// 	} `json:"name3"`
	// 	Thing4 struct {
	// 		Value string `json:"value"`
	// 	} `json:"thing4"`
	// }

	// paramData.Phrase1.Value = "志愿者推出申请"
	// paramData.Phrase1.Value = act.Name
	// paramData.Phrase1.Value = act.Address
	// paramData.Phrase1.Value = act.StartTime

	// for _, item := range users {

	// 	paramData.Phrase1.Value = item.RealName
	// 	paramData.Phrase1.Value = item.MobileNumber

	// 	if err := message_queue.MessageQueueAdd(item.Openid, template_id, paramData, page); err != nil {
	// 		bean.Log.Error(err)
	// 		return
	// 	}
	// }

}

// 负责人对其他人签到
func Sign(c *gin.Context) {
	// 获取登录用户对象
	var u user.User
	if err := u.Get(c); err != nil {
		bean.Log.Error(err)
		c.String(500, err.Error())
		return
	}

	var req struct {
		UserId     uint   `binding:"max=10000000"`
		ActivityId uint   `binding:"max=10000000"`
		SignStatus string `binding:"oneof=未签到 已签到"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		bean.Log.Warning(err.Error())
		c.String(400, err.Error())
		return
	}

	// 验证是否是负责人
	var act activity.Activity
	if err := bean.MysqlDb.Where("`id`=?", req.ActivityId).
		First(&act).Error; err != nil {
		bean.Log.Error(err)
		c.String(400, err.Error())
		return
	}

	// 判断是否是负责人
	if act.ResponsibleUserId != u.ID {
		c.String(400, "没有该权限")
		return
	}

	var one activity.ActivityUser
	if err := bean.MysqlDb.Where("`activity_id`=?", req.ActivityId).
		Where("`user_id`=?", req.UserId).
		First(&one).Error; err != nil {
		bean.Log.Error(err)
		c.String(400, err.Error())
		return
	}

	// 计算报名截止时间
	EndTime, err := time.ParseInLocation(time.DateTime, act.EndTime, time.Local)
	if err != err {
		var err = fmt.Sprintf("时间解析失败:%s", act.EndTime)
		bean.Log.Error(err)
		c.String(500, err)
		return
	}

	// 活动已结束
	if time.Now().After(EndTime) {
		bean.Log.Error("code:5138 活动已结束")
		c.String(400, "code:5138 活动已结束")
		return
	}

	var two activity.ActivityUser
	two.ID = one.ID
	two.SignStatus = req.SignStatus

	if err := bean.MysqlDb.Updates(&two).Error; err != nil {
		bean.Log.Error(err)
		c.String(500, "数据库错误")
		return
	}

	c.PureJSON(200, one)
}
